﻿using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NReader.Service;

namespace XnReader.WinRT.Helper
{
    public class SearchArticleListPage<T> : IncrementalLoadingBase
    {

        public SearchArticleListPage(uint maxCount, string queryString)
        {
            _maxCount = maxCount;
            _queryString = queryString;
        }

        protected async override Task<IList<object>> LoadMoreItemsOverrideAsync(CancellationToken c, uint count)
        {
            uint toGenerate = System.Math.Min(count, _maxCount - _count);

            // Wait for work 
            await Task.Delay(10);

            var values = await ArticleService.Instance().SearchArticleList(_queryString, (int)_count, (int)toGenerate);
            _count += toGenerate;

            return values.ToArray();
        }

        protected override bool HasMoreItemsOverride()
        {
            return _count < _maxCount;
        }

        #region State

        uint _count = 0;
        string  _queryString ;
        uint _maxCount;

        #endregion 
    }
}
